package com.ourteam.frame.web;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import net.dao.BaseQueryBean;
import net.dao.BaseQueryBean.SelectProperty;
import net.ui.UIConfigFactory;
import net.ui.model.table.ColumnBean;
import net.ui.model.table.TableBean;

abstract public class AbstractSearchModel<T extends BaseQueryBean> implements Serializable {

	private String tableConfigId;

	private int page;

	private int rows;

	private String sidx;

	private String sord;

	private int iDisplayLength;

	private int iDisplayStart;

	private int sEcho;

	private String[] sortProperties = new String[] {};

	private String[] sortDirs = new String[] {};

	public String[] getSortProperties() {
		return sortProperties;
	}

	public void setSortProperties(String[] sortProperties) {
		this.sortProperties = sortProperties;
	}

	public String[] getSortDirs() {
		return sortDirs;
	}

	public void setSortDirs(String[] sortDirs) {
		this.sortDirs = sortDirs;
	}

	public int getsEcho() {
		return sEcho;
	}

	public void setsEcho(int sEcho) {
		this.sEcho = sEcho;
	}

	public int getiDisplayLength() {
		return iDisplayLength;
	}

	public void setiDisplayLength(int iDisplayLength) {
		this.iDisplayLength = iDisplayLength;
		this.rows = iDisplayLength;
	}

	public int getiDisplayStart() {
		return iDisplayStart;
	}

	public void setiDisplayStart(int iDisplayStart) {
		this.iDisplayStart = iDisplayStart;

	}

	public int getPage() {
		if (this.iDisplayLength == 0) {
			return 0;
		}
		this.page = (iDisplayStart / this.iDisplayLength);
		return page;
	}

	public void setPage(int page) {
		this.page = page;

		if (this.page == 0) {
			this.page = 1;
		}
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;

		if (this.rows == 0) {
			this.rows = 10;
		}
	}

	public String getSidx() {
		return sidx;
	}

	public void setSidx(String sidx) {
		this.sidx = sidx;
	}

	public String getSord() {
		return sord;
	}

	public void setSord(String sord) {
		this.sord = sord;
	}

	public int getStart() {
		return this.iDisplayStart;
	}

	public void setOffset(int offset) {
		this.iDisplayStart = offset;
	}

	public void setLimit(int limit) {
		this.iDisplayLength = limit;
		this.rows = limit;
	}

	public String getTableConfigId() {
		return tableConfigId;
	}

	public void setTableConfigId(String tableConfigId) {
		this.tableConfigId = tableConfigId;
	}

	public void buildPageQueryBean(T queryBean) throws Exception {

		if (getStart() >= 0 && getRows() > 0) {

			queryBean.setStart(getStart());

			queryBean.setMaxResults(getRows());
		}

		if (ArrayUtils.isNotEmpty(sortProperties) && ArrayUtils.isNotEmpty(sortDirs)) {
			for (int i = 0; i < sortProperties.length; i++) {
				//System.out.println(sortProperties[i] + " - " + sortDirs[i]);
				queryBean.setSortProperty(getMappedDAOField(sortProperties[i]), "asc".equalsIgnoreCase(sortDirs[i]));
			}

		}

		if (StringUtils.isNotBlank(tableConfigId)) {
			TableBean tableBean = UIConfigFactory.getInstance(tableConfigId).getTableConfig(tableConfigId);

			ColumnBean[] columnBeans = tableBean.getColumns();

			for (int i = 0; i < columnBeans.length; i++) {

				if (StringUtils.isNotBlank(getMappedDAOField(columnBeans[i].getProperty()))) {
					queryBean.addSelectProperty(getMappedDAOField(columnBeans[i].getProperty()),
							columnBeans[i].getProperty());
				}

			}
			List<SelectProperty> selectList = queryBean.getSelectProperties();

			boolean hasId = false;

			for (Iterator<SelectProperty> iterator = selectList.iterator(); iterator.hasNext();) {
				SelectProperty selectProperty = iterator.next();
				if (selectProperty.getAlias().equals("id")) {
					hasId = true;
				}
			}

			if (hasId == false && StringUtils.isNotBlank("id")) {
				queryBean.addSelectProperty(getMappedDAOField("id"), "id");
			}

		}

		doBuildQueryBean(queryBean);

	}

	public void buildQueryBean(T queryBean) throws Exception {

		doBuildQueryBean(queryBean);

	}

	abstract public String getMappedDAOField(String aProperty);

	abstract protected void doBuildQueryBean(T queryBean) throws Exception;

}
